package com.atom.app;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atom.annotation.ApiImpl;
import com.atom.annotation.api.ApiCLassFilter;
import com.atom.annotation.api.ApiImplCallback;
import com.atom.annotation.api.ApiImplContext;
import com.atom.annotation.bean.ApiImplWrapper;
import com.atom.annotation.bean.ApiImpls;
import com.atom.base.app.Hello;
import com.atom.app.base.AbstractFragment;
import com.atom.module.logger.Logger;

import java.util.Collection;

@ApiImpl(api = AbstractFragment.class, name = "main/menu/api")
public class TestAtomApiFragment extends AbstractFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_test_api, container, false);
        initView(inflate);
        return inflate;
    }

    void initView(View layout) {
        /**
         * 进行根据api获取所有模块实现该api的class
         */
        layout.findViewById(R.id.test_class_api_list).setOnClickListener(v -> {
            apiImplContext().getApis(Hello.class, new ApiImplCallback<Collection<Class<? extends Hello>>>() {
                @Override
                public void call(ApiImplContext apiImplContext, Collection<Class<? extends Hello>> classes) {
                    StringBuilder builder = new StringBuilder('\n');
                    builder.append("implClass / name / version").append('\n');
                    for (Class<? extends Hello> item : classes
                    ) {
                        ApiImpl annotation = item.getAnnotation(ApiImpl.class);
                        if (annotation == null) continue;
                        builder.append(item.getCanonicalName())
                                .append(" / ")
                                .append(annotation.name())
                                .append(" / ")
                                .append(annotation.version())
                                .append('\n');
                    }
                    String s = builder.toString();
                    Logger.e(s);
                }
            });
        });
        /**
         * 进行根据api 以及 name 精准获取 class 或者根据 正则表达式 模糊定位
         */
        layout.findViewById(R.id.test_class_api_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 精确查找
                final long l = System.currentTimeMillis();
                Collection<Class<? extends Hello>> hello2 = apiImplContext().getApis(Hello.class, "hello2", false);
                final long space = System.currentTimeMillis() - l;
                // 正则查找
                //Collection<Class<? extends Hello>> hello2 = apiImplContext().getApis(Hello.class, "hello(.*)", true);
                StringBuilder builder = new StringBuilder('\n');
                builder.append("implClass / name / version").append('\n');
                for (Class<? extends Hello> item : hello2
                ) {
                    ApiImpl annotation = item.getAnnotation(ApiImpl.class);
                    if (annotation == null) continue;
                    builder.append(item.getCanonicalName())
                            .append(" / ")
                            .append(annotation.name())
                            .append(" / ")
                            .append(annotation.version())
                            .append('\n');
                }
                String s = builder.toString();
                Logger.e(" \n time = " + space, s);
            }
        });


        /**
         * 进行根据api 以及 name 精准获取 class 或者根据 正则表达式 模糊定位
         */
        layout.findViewById(R.id.test_class_api_filter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Collection<Class<? extends Hello>> hello2 = apiImplContext().getApis(Hello.class, new ApiCLassFilter<Hello>() {
                    @Override
                    public boolean accept(@NonNull ApiImplContext apiImplContext, @NonNull Class<? extends Hello> clazz, @NonNull ApiImplWrapper<Hello> param) {
                        if (param.getVersion() > 2) {
                            return true;
                        }
                        return false;
                    }
                });
                StringBuilder builder = new StringBuilder('\n');
                builder.append("implClass / name / version").append('\n');
                for (Class<? extends Hello> item : hello2
                ) {
                    ApiImpl annotation = item.getAnnotation(ApiImpl.class);
                    if (annotation == null) continue;
                    builder.append(item.getCanonicalName())
                            .append(" / ")
                            .append(annotation.name())
                            .append(" / ")
                            .append(annotation.version())
                            .append('\n');
                }
                String s = builder.toString();
                Log.e("TestAtomClassFragment", s);

                Logger.e(s);
            }
        });

        /**
         * 进行根据api 以及 name 精准获取 class 或者根据 正则表达式 模糊定位
         */
        layout.findViewById(R.id.test_class_api_single).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final long l = System.currentTimeMillis();
                final Class<? extends Hello> hello2 = apiImplContext().getApi(Hello.class, "hello2", 0, false);
                final long space = System.currentTimeMillis() - l;
                StringBuilder builder = new StringBuilder('\n');
                builder.append("implClass / name / version").append('\n');
                ApiImpl annotation = hello2.getAnnotation(ApiImpl.class);
                if (annotation == null) return;
                builder.append(hello2.getCanonicalName())
                        .append(" / ")
                        .append(annotation.name())
                        .append(" / ")
                        .append(annotation.version())
                        .append('\n')
                        .append("time = ").append(space);
                String s = builder.toString();
                Logger.e(s);
            }
        });
    }
}
